﻿// -----------------------------
// Copyright © Kodi Studios 2012
// -----------------------------

using System;
using System.Collections.Generic;

namespace Kodi.Picturethrill
{
    public class CommandLineArgumentParser
    {
        public static void Parse(string[] args, IList<CommandLineArgumentFlag> flags)
        {
            Parse(new List<string>(args), flags);
        }

        public static void Parse(IList<string> args, IList<CommandLineArgumentFlag> flags)
        {
            for (int i = 0; i < args.Count; i++)
            {
                foreach (CommandLineArgumentFlag parser in flags)
                {
                    if ("/" + parser.flag == args[i] || "-" + parser.flag == args[i])
                    {
                        if (parser.extraDataPresent)
                        {
                            i++;
                            parser.data = args[i];
                            if (parser.ActionT != null)
                            {
                                parser.ActionT(args[i]);
                            }
                        }
                        else
                        {
                            if (parser.Action != null)
                            {
                                parser.Action();
                            }
                        }
                        break;
                    }
                }
            }
        }
    }

    public class CommandLineArgumentFlag
    {
        #region Fields
        public bool Exists;
        public string flag;
        public Action Action;
        public Action<string> ActionT;
        public string data;
        public bool extraDataPresent;
        #endregion

        #region Public Constructors
        public CommandLineArgumentFlag(string flag)
            : this(flag, extraDataPresent: false)
        {
        }

        public CommandLineArgumentFlag(string flag, Action action)
            : this(flag, false, action)
        {
        }

        public CommandLineArgumentFlag(string flag, bool extraDataPresent)
            : this(flag, extraDataPresent, (Action)null)
        {
        }

        private CommandLineArgumentFlag(string flag, bool extraDataPresent, Action action)
        {
            this.flag = flag;
            this.extraDataPresent = extraDataPresent;
            this.Action = action;
        }

        public CommandLineArgumentFlag(string flag, Action<string> action)
        {
            this.flag = flag;
            this.extraDataPresent = true;
            this.ActionT = action;
        }

        public CommandLineArgumentFlag(string flag, bool extraDataPresent, Action<string> action)
        {
            this.flag = flag;
            this.extraDataPresent = extraDataPresent;
            this.ActionT = action;
        }
        #endregion
    }
}
